<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$return['setting']=$this->plugin_setting('basic');
	$return['list']=array();
	set_html('title','商品核销');
	$mid = $_SESSION['uid'];
	$verifycode = trim($_RQ['verifycode']);
	$id = trim($_RQ['id']);
	if (empty($verifycode)) 
	{
		exi('请输入核销码!','error','jy_merch/verify.verifygoods/');
	}
	$item = pdo_get('jy_weishop_verifygoods',array('uniacid' => $_SESSION['uniacid'], 'verifycode' => $verifycode));
	if (empty($item)) 
	{
		exi('未查询到记次时商品或核销码已失效,请核对核销码!', 'error','jy_merch/verify.verifygoods/');
	}
	//获取订单状态
	$order = pdo_get('jy_weishop_order',array('id'=>$item['orderid'],'uniacid' => $_SESSION['uniacid']));
	$orderstatus = $order['status'];
	if($orderstatus==4){
		exi('订单已关闭，不能核销！','error','jy_merch/verify.verifygoods/');
	}elseif($orderstatus==5){
		exi('订单已申请退款，不能核销！','error','jy_merch/verify.verifygoods/');
	}elseif($orderstatus==5){
		exi('订单已退款，不能核销！','error','jy_merch/verify.verifygoods/');
	}
	if(!$order['merchid']){
		exi('非商户订单!','error','jy_merch/verify.verifygoods/');
	}
	//核销店员身份
	$prams=array('status'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	$prams['merchid']=$order['merchid'];
	$store=$this->model('verify')->checkuser($_SESSION['uid'],$order['merchid'],'jy_merch/verify.verifygoods/');
	if ($item['limitnum']){
		$verifygoodlogs = pdo_getall('jy_weishop_verifygoods_log',array('verifygoodsid'=>$item['id']));
		$verifynum = 0;
		foreach ($verifygoodlogs as $verifygoodlog ) 
		{
			$verifynum += intval($verifygoodlog['verifynum']);
		}
		$lastverifys = intval($item['limitnum']) - intval($verifynum);
		if ($lastverifys < 1) 
		{
			exi('核销次数已用完!', 'error','jy_merch/verify.verifygoods/');
		}
	}
	if (empty($item['limittype'])) {
		$item['limitdays']=$item['limitdays']?:365;
		$limitdate = intval($item['starttime']) + (intval($item['limitdays']) * 86400);
	}else{
		$limitdate = intval($item['limitdate']);
	}
	if ($limitdate < time()) {
		exi('该商品已过期!', 'error','jy_merch/verify.verifygoods/');
	}
	$return['post']=array(
		'orderid'=>$item['orderid'],
		'num'=>1,
		'verifycode'=>$verifycode,
	);
	$goods=pdo_get('jy_weishop_goods',array('uniacid' => $_SESSION['uniacid'], 'id' => $item['ordergoodsid']),array('image','title','subtitle'));
	$item['optionid']=pdo_getcolumn('jy_weishop_order_goods',array('uniacid' => $_SESSION['uniacid'],'goodsid' => $item['ordergoodsid'],'orderid' => $item['orderid']),'optionid');
	//商品规格
	if($item['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$item['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goods['selespename']=implode("，",$specs);
	}
	$item=array_merge($item,$goods);
	$termofvalidity = date('Y-m-d H:i', $limitdate);
	$return['item']=$item;
	$return['saler']=$saler;
	$return['store']=$store;
	$return['lastverifys']=$lastverifys;
	$return['termofvalidity']=$termofvalidity;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$orderid=intval($post['orderid']);
	$num=intval($post['num']);
	$verify_code=trim($post['verifycode']);
	$remarks=trim($post['remarks']);
	model('jy_weishop/order')->order_verifygoods($verify_code,$_SESSION['uid'],$num,$remarks);
	exi('核销成功！','','jy_merch/verify.verifygoods/');
	
}
?>